package cn.yitun.chat.yitunservermanagecore.general.manage.core.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.login.LoginLogDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.login.LoginLogVo;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.core.service.LoginLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage/core")
public class LoginLogController extends BaseController {

    @Autowired
    private LoginLogService loginService;

    /**
     * 列表 分页
     * @param dto
     * @return
     */
    @RequestParameter
    @PermissionMapping(name = "列表", perms = "sys:loginLog:loginLog")
    @PostMapping("/loginLog/page")
    public ResultMessage list(JwtUserDto jwtUserDto, @JsonDefine LoginLogDto dto ){
        //查询列表数据
        checkRootPage(dto, jwtUserDto);
        dto.setOperatorId(jwtUserDto.getUserId());
        PageHelper.startPage(dto.getCurrentPageNo() , dto.getPageSize());
        List<LoginLogVo> logList = loginService.listLoginLog(dto);
        ResultMessage rm = new ResultMessage();
        rm.bodyPut("page", new PageInfo<>(logList));
        return rm;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestParameter
    @PermissionMapping(name = "列表", perms = "sys:loginLog:remove")
    @DeleteMapping( "/loginLog/remove/{id}")
    public ResultMessage remove( @PathVariable("id") Long id){
        if( loginService.remove(id) == 0 )
            throw new YitunException(ExceptionEnum.DELETE_FAILED);

        return new ResultMessage();
    }

    /**
     * 批量删除
     */
    @RequestParameter
    @PermissionMapping(name = "列表", perms = "sys:loginLog:remove")
    @DeleteMapping( "/loginLog/batchRemove/{ids}")
    public ResultMessage remove(@PathVariable("ids") Long[] ids){
        if( loginService.batchRemove(ids) == 0 )
            throw new YitunException(ExceptionEnum.DELETE_FAILED) ;

        return new ResultMessage();
    }

}
